package com.zxu.test.start;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.Observable;
import androidx.lifecycle.ViewModelProviders;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.zxu.test.EventBusUtil.AppEventMsg;
import com.zxu.test.EventBusUtil.BroadcastEvent;
import com.zxu.test.RxUtil.RxBus;
import com.zxu.test.RxUtil.RxEventMsg;
import com.zxu.test.activity.ActionActivity;
import com.zxu.test.activity.TryActivity;
import com.zxu.test.base.BaseFragment;
import com.zxu.test.grav.GravActivity;
import com.zxu.test.ipc.client.ClientActivity;
import com.zxu.test.studyapp.MainActivity;
import com.zxu.test.studyapp.databinding.ActivitySplashBinding;
import com.zxu.test.util.AcUtils;
import com.zxu.test.util.Event;
import com.zxu.test.util.IntentMsg;
import com.zxu.test.util.StrUtil;
import com.zxu.test.view.ToastCustom;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class SplashFragment extends BaseFragment {
    private ActivitySplashBinding mBinding;
    private SplashViewModel mViewModel;
    private LocalBroadcastManager lbm;

    public SplashFragment() { }

    public static SplashFragment getInstance() {
        return new SplashFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = ActivitySplashBinding.inflate(inflater, container, false);
        mViewModel = ViewModelProviders.of(getActivity()).get(SplashViewModel.class);
        mBinding.setViewModel(mViewModel);
        EventBus.getDefault().register(this);
        lbm = LocalBroadcastManager.getInstance(getContext());
        return  mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupListener();
        setBroadCastEvent();
        initRx();
    }

    private void setupListener() {
        mViewModel.getBroadcastCommand().observe(getViewLifecycleOwner(), this::goToBrodcast);
        mViewModel.btnEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                IntentMsg intentMsg = new IntentMsg();
                intentMsg.Id ="1";
                AcUtils.launchActivity(getContext(), MainActivity.class, intentMsg);
            }
        });
        mViewModel.tryEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                IntentMsg intentMsg = new IntentMsg();
                intentMsg.Id ="2";
                AcUtils.launchActivity(getContext(), TryActivity.class, intentMsg);
            }
        });
        mViewModel.gravEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                AcUtils.launchActivity(getContext(), GravActivity.class, null);
            }
        });
        mViewModel.clientEvent.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                AcUtils.launchActivity(getContext(), ClientActivity.class, null);
            }
        });

    }

    private void setBroadCastEvent() {
        lbm.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (intent.getAction()){
                    case BroadcastEvent.SEND_LOCALBROADCAST_MSG:
                        ToastCustom.getInstance(getContext()).show("SEND_LOCALBROADCAST_MSG",1000);
                        break;
                }

            }
        },new IntentFilter(BroadcastEvent.SEND_LOCALBROADCAST_MSG));
    }
    private void goToBrodcast(Event<Object> taskIdEvent) {
        IntentMsg intentMsg = new IntentMsg();
        intentMsg.Action_Msg = StrUtil.ACTIONACTIVITY_ACTION;
        AcUtils.launchActionActivity(getContext(), StrUtil.ACTIONACTIVITY_ACTION, intentMsg);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSubscribe(AppEventMsg event) {
        if (event.getMsg() == AppEventMsg.SPLASH_FRAGMENT_MSG){
            ToastCustom.getInstance(getContext()).show("SPLASH_FRAGMENT_MSG",1000);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //不是太好用，了解一下有这么个关于处理事件的方式
    public void initRx() {
        RxBus.getInstance().toObservable(RxEventMsg.class).subscribe(new Observer<RxEventMsg>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
            @Override
            public void onNext(RxEventMsg rxEventMsg) {
                Log.i("zzxuu","onNext" + rxEventMsg.getMsg().toString());
                if(rxEventMsg.getMsg() == RxEventMsg.RXBUS_TRY_MSG){
                    Log.i("zzxuu","牛皮");
                }
            }
            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {
            }
        });
    }
}
